#! /usr/bin/perl
use strict;
use warnings;

my $file = "/home/hectorbernal/Dropbox/perl_tests/city_reader/cities.txt";
my %cities;
open FILE, ">>$file" or die $!;
while(my $line = <FILE>){
	chomp($line);
	my ($city, $country) = split(/,/, $line);
	if(defined($cities{$country})){
		push(@{$cities{$country}}, $city);
	}else{
		my $list = [$city];
		$cities{$country}=$list;	
	}
}
while ( my ($key, $value) = each(%cities) ) {
        print FILE "$key => ";
	print FILE join ', ', sort @{$value};
	print FILE"\n";
    }
close FILE;
